using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Tools;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Framework.Verifications;
using Pixysoft.Framework.Noebe;
using Pixysoft.Weblications.Sango.Entities;
using Pixysoft.Weblications.Sango.Constant;

namespace Pixysoft.Weblications.Sango.Environment
{

    public class WarStageHelper
    {
        private static List<War_Stage> stages = new List<War_Stage>();

        private static List<War_Map> maps = new List<War_Map>();


        public static void initialize()
        {
            stages = SettingHelper.GetNoebe().EntityNoebeCommand.Select<War_Stage>();

            LoggerHelper.Info("initialize War_Stage.count{0}", stages.Count);

            maps = SettingHelper.GetNoebe().EntityNoebeCommand.Select<War_Map>();

            LoggerHelper.Info("initialize War_Map.count{0}", maps.Count);
        }



        public static List<War_Stage> searchStages()
        {
            return stages;
        }

        public static War_Stage searchStageByCode(Int64 serialcode)
        {
            foreach (War_Stage orm in stages)
            {
                if (orm.Stageserialcode == serialcode)
                    return orm;
            }

            throw Exceptions.VerificationFailedException(serialcode);
        }

        public static List<War_Stage> searchStageByIndex(long mapserialcode, long stageindex)
        {
            List<War_Stage> list = new List<War_Stage>();

            foreach (War_Stage orm in stages)
            {
                if (orm.Mapserialcode != mapserialcode)
                    continue;

                if (orm.Stageindex != stageindex)
                    continue;

                list.Add(orm);
            }

            return list;
        }

        public static War_Stage searchFinishStage(War_Map map)
        {
            foreach (War_Stage child in stages)
            {
                if (child.Mapserialcode != map.Mapserialcode)
                    continue;

                if (child.Locationtypecode == WarStageLocationTypecodeCollection.FINISH)
                    return child;
            }

            throw Exceptions.VerificationFailedException(map.Mapserialcode);
        }

        public static War_Stage searchStartStage(long mapserialcode)
        {
            foreach (War_Stage child in stages)
            {
                if (child.Mapserialcode != mapserialcode)
                    continue;

                if (child.Locationtypecode == WarStageLocationTypecodeCollection.START)
                    return child;
            }

            throw Exceptions.VerificationFailedException(mapserialcode);
        }

        public static War_Stage searchPassStage(War_Map map)
        {
            foreach (War_Stage child in stages)
            {
                if (child.Mapserialcode != map.Mapserialcode)
                    continue;

                if (child.Locationtypecode == WarStageLocationTypecodeCollection.PASS)
                    return child;
            }

            throw Exceptions.VerificationFailedException(map.Mapserialcode);
        }

        public static List<War_Stage> searchStagesByMap(long mapserialcode)
        {
            List<War_Stage> list = new List<War_Stage>();

            foreach (War_Stage stage in stages)
            {
                if (stage.Mapserialcode != mapserialcode)
                    continue;

                list.Add(stage);
            }

            return list;
        }



        public static List<War_Map> searchMaps()
        {
            return maps;
        }

        public static War_Map searchMap(Int64 serialcode)
        {
            foreach (War_Map orm in maps)
            {
                if (orm.Mapserialcode == serialcode)
                    return orm;
            }

            throw Exceptions.VerificationFailedException(serialcode);
        }

        public static War_Map getStartMap()
        {
            foreach (War_Map map in maps)
            {
                if (map.Parentmapserialcode < 0)
                    return map;
            }

            throw Exceptions.VerificationFailedException();
        }

        public static War_Map tryGetParentMap(War_Map child)
        {
            foreach (War_Map map in maps)
            {
                if (map.Mapserialcode == child.Parentmapserialcode)
                    return map;
            }

            return null;
        }

        public static List<War_Map> getChildMapsBySerialcode(Int64 serialcode)
        {
            List<War_Map> children = new List<War_Map>();

            foreach (War_Map map in maps)
            {
                if (serialcode == map.Parentmapserialcode)
                    children.Add(map);
            }

            return children;
        }
    }
}